﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Resources;
using System.Globalization;
using System.Text;

namespace PHTShop
{
    public partial class WebForm1 : basePageUser
    {
       

        
        
        private DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {

            switch (Session["languages"].ToString().ToLower())
            { 
                case "france":
                    Page.Title = "Maison";
                    break;
                case "germany":
                    Page.Title = "Zuhause";
                    break;
                default:
                    Page.Title = "Home";
                    break;
            }
          
                this.ProductLanguage();
                this.NewProductLanguage();
            
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
                Setting th = this.dc.Settings.Single(t => t.type.Equals("theme") && Convert.ToInt32(t.value) == 1);
                Page.Theme = th.name.ToString();    
        }

        private void ProductLanguage()
        {
            Language lg = this.dc.Languages.Single(t => t.name.Equals(Session["languages"]));

            List<Product> products = this.dc.Products.Where(t => t.status == true).ToList();
            List<ClassLoadProducts> list = new List<ClassLoadProducts>();

            foreach (var i in products)
            {
                Product pro = this.dc.Products.Single(t => t.id == i.id);
                ProductsLanguage prolg = this.dc.ProductsLanguages.Single(t => t.languageId == lg.id && t.productsId == i.id);
                string photoName = "";

                List<ProductsPhoto> productsPhoto = this.dc.ProductsPhotos.Where(t => t.productsId == i.id).ToList();
                foreach (var j in productsPhoto)
                {
                    ProductsPhoto propt = this.dc.ProductsPhotos.Single(t => t.id == j.id);
                    photoName = propt.name;
                }

                list.Add(new ClassLoadProducts(pro, prolg, photoName));
            }

            this.RepeaterProduct.DataSource = list.Take(2);
            this.RepeaterProduct.DataBind();

        }


        private void NewProductLanguage()
        {
            Language lg = this.dc.Languages.Single(t => t.name.Equals(Session["languages"]));

            List<Product> products = this.dc.Products.Where(t => t.status == true).OrderByDescending(t=>t.id).ToList();
            List<ClassLoadProducts> list = new List<ClassLoadProducts>();

            foreach (var i in products)
            {
                Product pro = this.dc.Products.Single(t => t.id == i.id);
                ProductsLanguage prolg = this.dc.ProductsLanguages.Single(t => t.languageId == lg.id && t.productsId == i.id);
                string photoName = "";

                List<ProductsPhoto> productsPhoto = this.dc.ProductsPhotos.Where(t => t.productsId == i.id).ToList();
                foreach (var j in productsPhoto)
                {
                    ProductsPhoto propt = this.dc.ProductsPhotos.Single(t => t.id == j.id);
                    photoName = propt.name;
                }

                list.Add(new ClassLoadProducts(pro, prolg, photoName));
            }

            this.RepeaterNewProduct.DataSource = list.Take(3);
            this.RepeaterNewProduct.DataBind();

            
        }
    }
}